package UILayer;

import CtrLayer.OrderCtr;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DiscountUI extends JInternalFrame
{
	private JTextField max_dis_tf;
	private JTextField pickup_dis_tf;
	private OrderCtr order_ctr;
	private JPanel panel;
	private JLabel wrong_max_lbl;
	private JLabel wrong_pick_lbl;

	public DiscountUI()
	{
		super("Manage discounts", true, true, true, true);
		setBounds(200, 100, 450, 300);
		order_ctr = new OrderCtr();
		panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		make_lbls();
		make_tfs();
		make_ctr_btns();
		
	}
	
	private void make_lbls()
	{
		JLabel lblInsertDiscounts = new JLabel("Insert discounts.");
		lblInsertDiscounts.setFont(new Font("Times New Roman", Font.BOLD, 14));
		lblInsertDiscounts.setBounds(10, 11, 121, 14);
		panel.add(lblInsertDiscounts);
		
		JLabel max_dis_lbl = new JLabel("Maximum discount");
		max_dis_lbl.setBounds(10, 75, 99, 14);
		panel.add(max_dis_lbl);
		
		JLabel pickup_dis_lbl = new JLabel("Pickup discount");
		pickup_dis_lbl.setBounds(10, 132, 84, 14);
		panel.add(pickup_dis_lbl);
		
		JLabel percent_2_lbl = new JLabel("%");
		percent_2_lbl.setBounds(215, 132, 11, 14);
		panel.add(percent_2_lbl);
		
		JLabel percent_1_lbl = new JLabel("%");
		percent_1_lbl.setBounds(215, 75, 11, 14);
		panel.add(percent_1_lbl);
		
		wrong_max_lbl = new JLabel("The discount was inserted incorrectly.");
		wrong_max_lbl.setForeground(Color.RED);
		wrong_max_lbl.setBounds(236, 75, 188, 14);
		wrong_max_lbl.setVisible(false);
		panel.add(wrong_max_lbl);
		
		wrong_pick_lbl = new JLabel("The discount was inserted incorrectly.");
		wrong_pick_lbl.setForeground(Color.RED);
		wrong_pick_lbl.setBounds(236, 132, 188, 14);
		wrong_pick_lbl.setVisible(false);
		panel.add(wrong_pick_lbl);
		
		
	}
	
	private void make_tfs()
	{
		max_dis_tf = new JTextField();
		max_dis_tf.setBounds(119, 72, 86, 20);
		max_dis_tf.setText(order_ctr.display_maximum_discount());
		panel.add(max_dis_tf);
		max_dis_tf.setColumns(10);
		
		pickup_dis_tf = new JTextField();
		pickup_dis_tf.setBounds(119, 129, 86, 20);
		pickup_dis_tf.setText(order_ctr.display_pickup_discount());
		panel.add(pickup_dis_tf);
		pickup_dis_tf.setColumns(10);
		
		
	}
	
	private void make_ctr_btns()
	{
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JButton ok_btn = new JButton("Ok");
		ok_btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { change(); } });
		panel_1.add(ok_btn, BorderLayout.WEST);
		
		JButton cancel_btn = new JButton("Cancel");
		cancel_btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } });
		panel_1.add(cancel_btn, BorderLayout.EAST);
	}
	
	private void change()
	{
		
		wrong_max_lbl.setVisible(false);
		wrong_pick_lbl.setVisible(false);
		boolean ok = true;
		String md = max_dis_tf.getText();
		String pd = pickup_dis_tf.getText();
		if(!(double_convertable(md)))
		{
			wrong_max_lbl.setVisible(true);
			ok = false;
		}
		if(!(double_convertable(pd)))
		{
			wrong_pick_lbl.setVisible(true);
			ok = false;
		}
		if(ok)
		{
			if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this,
					"Are you sure you want to set discounts?",
					"Set discounts", JOptionPane.YES_NO_OPTION))
			{
				order_ctr.change_maximum_discount(Double.parseDouble(md));
				order_ctr.change_pickup_discount(Double.parseDouble(pd));
				JOptionPane.showMessageDialog(this,
						"Successully set discounts.", "Success",
						JOptionPane.INFORMATION_MESSAGE);
				dispose();
			}
		}
	}
	
	private boolean double_convertable(String data)
	{
		try
		{
			double d = Double.parseDouble(data);
			if (d < 0 || d >= 100)
			{
				return false;
			}
		}
		catch (NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}
}
